﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspCategoriesEdit
{
    public partial class Edit : System.Web.UI.Page
    {
        private int id;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.Params["id"]);
            if (!Page.IsPostBack)
            {
                var res = DataLayer.FindCategoryById(id);
                if (res != null)
                {
                    nameTxt.Text = res.CategoryName;
                    descriptionTxt.Text = res.Description;
                }
            }
        }

        protected void saveBtn_Click(object sender, EventArgs e)
        {
            if (nameTxt.Text != "" && nameTxt.Text.Length <= 15)
            {
                DataLayer.Edit(id, nameTxt.Text, descriptionTxt.Text);
                Response.Redirect("Default.aspx");
            }
            else
            {
                error.Text = "Invalid Category Name";
            }   
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}